import request from '@/utils/request'

/**
 * 获取考试活动列表
 * @param {Object} params 查询参数
 * @param {number} params.examGroupId 考试组ID
 * @param {number} [params.type] 活动类型(1-考试,2-测评,3-作业)
 * @param {number} [params.status] 状态(0-草稿,1-已发布,2-进行中,3-已结束,4-已归档)
 * @param {PageQuery} pageQuery 分页参数
 * @returns {Promise<TableDataInfo<ExamActivityVo>>}
 */
export function getExamActivities(params) {
  return request({
    url: `/exam/activity/list`,
    method: 'get',
    params: {
      ...params,
    }
  })
}

export function getStuListOfActivity(params) {
  return request({
    url: `/exam/activity/student/list`,
    method: 'get',
    params: params
  })
}

/**
 * 获取考试活动详情
 * @param {number} id 活动ID
 * @returns {Promise<R<ExamActivityDetailVo>>}
 */
export function getExamDetail(id) {
  return request({
    url: `/exam/activity/${id}`,
    method: 'get'
  })
}

/**
 * 创建考试活动
 * @param {ExamActivityBo} data 活动数据
 * @returns {Promise<R<Long>>} 返回活动ID
 */
export function addExam(data) {
  return request({
    url: '/exam/activity',
    method: 'post',
    data
  })
}

/**
 * 更新考试活动
 * @param {ExamActivityBo} data 活动数据
 * @returns {Promise<R<Void>>}
 */
export function updateExam(data) {
  return request({
    url: '/exam/activity',
    method: 'put',
    data
  })
}

/**
 * 删除考试活动
 * @param {number} id 活动ID
 * @returns {Promise<R<Void>>}
 */
export function deleteExam(id) {
  return request({
    url: `/exam/activity/${id}`,
    method: 'delete'
  })
}

/**
 * 批量更新活动状态
 * @param {Array<number>} ids 活动ID数组
 * @param {number} status 目标状态
 * @returns {Promise<R<Void>>}
 */
export function updateStatus(ids, status) {
  return request({
    url: `/exam/activity/status`,
    method: 'put',
    params: { ids: ids.join(','), status }
  })
}

/**
 * 获取考试组的分组成员树
 * @param {number} groupId 考试组ID
 * @returns {Promise<R<Array<ExamSubgroupVo>>>}
 */
export function getExamSubgroups(groupId) {
  return request({
    url: `/exam/group/subgroup/list/${groupId}`,
    method: 'get'
  })
}

/**
 * 获取考试组成员列表
 * @param {number} groupId 考试组ID
 * @param {string} [searchKey] 搜索关键字
 * @param {PageQuery} pageQuery 分页参数
 * @returns {Promise<TableDataInfo<ExamMemberVo>>}
 */
export function getExamGroupMembers(groupId, searchKey, pageQuery) {
  return request({
    url: `/exam/group/member/stu`,
    method: 'get',
    params: {
      groupId,
      keyword: searchKey,
      pageNum: 1,
      pageSize: 20

    }
  })
}

/**
 * 获取试卷列表
 * @param {Object} params 查询参数
 * @param {string} [params.searchKey] 搜索关键字
 * @param {number} [params.status] 状态(0-禁用,1-启用)
 * @param {PageQuery} pageQuery 分页参数
 * @returns {Promise<TableDataInfo<PaperListVo>>}
 */
export function getPapers(params) {
  return request({
    url: '/paper2/list',
    method: 'get',
    params: {
      pageNum: params.pageNum || 1,
      pageSize: params.pageSize || 10,
      keyword: params.searchKey || ''  // 改为keyword与后端一致
    }
  })
}

/**
 * 预览试卷
 * @param {number} paperId 试卷ID
 * @param {PreviewOptions} options 预览选项
 * @returns {Promise<R<PaperPreviewVo>>}
 */
export function previewPaper(paperId, options = {}) {
  return request({
    url: `/paper2/preview/${paperId}`,
    method: 'get',
    params: {
      showAnswers: options.showAnswers !== false,
      showOptionAnswers: options.showOptionAnswers !== false,
      showAnalysis: options.showAnalysis !== false,
      showKnowledgePoints: options.showKnowledgePoints !== false,
      showDifficulty: options.showDifficulty !== false
    }
  })
}

/**
 * 导出考试活动统计数据
 * @param {number} examId 考试ID
 * @param {string} exportType 导出类型(pdf/excel)
 * @returns {Promise<void>}
 */
export function exportExamStats(examId, exportType) {
  return request({
    url: `/exam/export/${examId}`,
    method: 'get',
    params: { exportType },
    responseType: 'blob'
  })
}
